using System;
using System.Data;
using System.Data.SqlClient;
using Entity;

namespace SalesVMT
{
    public class CompanyDB
    {

        public static DataSet GetOne(int _Company_ID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Company_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Company_ID", _Company_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public static int Insert(CompanyInfo _CompanyInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Company_Insert", dbConn);
            dbCmd.Parameters.AddWithValue("@Company_ID", _CompanyInfo.Company_ID);
            dbCmd.Parameters.AddWithValue("@Company_Code", _CompanyInfo.Company_Code);
            dbCmd.Parameters.AddWithValue("@Company_Name", _CompanyInfo.Company_Name);
            dbCmd.Parameters.AddWithValue("@Company_Address", _CompanyInfo.Company_Address);
            dbCmd.Parameters.AddWithValue("@Company_Tel", _CompanyInfo.Company_Tel);
            dbCmd.Parameters.AddWithValue("@Company_Fax", _CompanyInfo.Company_Fax);
            dbCmd.Parameters.AddWithValue("@Company_Email", _CompanyInfo.Company_Email);
            dbCmd.Parameters.AddWithValue("@Company_Website", _CompanyInfo.Company_Website);
            dbCmd.Parameters.AddWithValue("@Company_Currency", _CompanyInfo.Company_Currency);
            dbCmd.Parameters.AddWithValue("@Company_City", _CompanyInfo.Company_City);
            dbCmd.Parameters.AddWithValue("@Company_PriceDifference", _CompanyInfo.Company_PriceDifference);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Update(CompanyInfo _CompanyInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Company_Update", dbConn);
            
            dbCmd.Parameters.AddWithValue("@Company_ID", _CompanyInfo.Company_ID);
            dbCmd.Parameters.AddWithValue("@Company_Code", _CompanyInfo.Company_Code);
            dbCmd.Parameters.AddWithValue("@Company_Name", _CompanyInfo.Company_Name);
            dbCmd.Parameters.AddWithValue("@Company_Address", _CompanyInfo.Company_Address);
            dbCmd.Parameters.AddWithValue("@Company_Tel", _CompanyInfo.Company_Tel);
            dbCmd.Parameters.AddWithValue("@Company_Fax", _CompanyInfo.Company_Fax);
            dbCmd.Parameters.AddWithValue("@Company_Email", _CompanyInfo.Company_Email);
            dbCmd.Parameters.AddWithValue("@Company_Website", _CompanyInfo.Company_Website);
            dbCmd.Parameters.AddWithValue("@Company_Currency", _CompanyInfo.Company_Currency);
            dbCmd.Parameters.AddWithValue("@Company_City", _CompanyInfo.Company_City);
            dbCmd.Parameters.AddWithValue("@Company_PriceDifference", _CompanyInfo.Company_PriceDifference);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static CompanyInfo GetInfo(int _Company_ID)
        {
            CompanyInfo retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Company_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Company_ID", _Company_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CompanyInfo();
                    retVal.Company_ID = Convert.ToInt32(dr["Company_ID"]);
                    retVal.Company_Code = Convert.ToString(dr["Company_Code"]);
                    retVal.Company_Name = Convert.ToString(dr["Company_Name"]);
                    retVal.Company_Address = Convert.ToString(dr["Company_Address"]);
                    retVal.Company_Tel = Convert.ToString(dr["Company_Tel"]);
                    retVal.Company_Fax = Convert.ToString(dr["Company_Fax"]);
                    retVal.Company_Email = Convert.ToString(dr["Company_Email"]);
                    retVal.Company_Website = Convert.ToString(dr["Company_Website"]);
                    retVal.Company_Currency = Convert.ToString(dr["Company_Currency"]);
                    retVal.Company_City = Convert.ToString(dr["Company_City"]);
                    retVal.Company_PriceDifference = Convert.ToDecimal(dr["Company_PriceDifference"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        
    }
}
